package com.rock.code.common.comm;

import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.security.SecureRandom;

/**
 * The type Des 3.
 *
 * @author kongqiang
 * @date 2017.03.30
 */
public class DES3 {
    /**
     * The constant DES.
     */
    private final static String DES = "DES";
    /**
     * The constant key.
     */
    private static String key = "rock!@#$%";

    /**
     * Main.
     *
     * @param args the args
     * @throws Exception the exception
     * @author kongqiang
     * @date 2017.03.30
     */
    public static void main(String args[]) throws Exception {
        String en = DES3.encode("123456");
        System.out.println(en);
        String de = DES3.decode("eyoLFkR9OtaRIiSr7Ti+pg==");
        System.out.println(de);
    }

    /**
     * Encode.
     *
     * @param code the code
     * @return the string
     * @author kongqiang
     * @date 2017.03.30
     */
    public static String encode(String code) {
        return encrypt(code,key);
    }

    /**
     * Decode.
     *
     * @param code the code
     * @return the string
     * @author kongqiang
     * @date 2017.03.30
     */
    public static String decode(String code){
        try{
            return decrypt(code,key);
        }catch(Exception ex){
            ex.printStackTrace();
            return code;
        }
    }

    /**
     * Encrypt.
     *
     * @param data the data
     * @param key  the key
     * @return the string
     * @author kongqiang
     * @date 2017.03.30
     */
    private static String encrypt(String data, String key) {
        byte[] bt = null;
        try{
            bt = encrypt(data.getBytes(), key.getBytes());
        }catch(Exception ex){
            ex.printStackTrace();
        }
        String strs = new BASE64Encoder().encode(bt);
        return strs;
    }

    /**
     * Decrypt.
     *
     * @param data the data
     * @param key  the key
     * @return the string
     * @throws IOException the io exception
     * @throws Exception   the exception
     * @author kongqiang
     * @date 2017.03.30
     */
    private static String decrypt(String data, String key) throws IOException,
            Exception {
        if (data == null)
            return null;
        BASE64Decoder decoder = new BASE64Decoder();
        byte[] buf = decoder.decodeBuffer(data);
        byte[] bt = decrypt(buf, key.getBytes());
        return new String(bt);
    }

    /**
     * Encrypt.
     *
     * @param data the data
     * @param key  the key
     * @return the byte [ ]
     * @throws Exception the exception
     * @author kongqiang
     * @date 2017.03.30
     */
    private static byte[] encrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();

        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher对象实际完成加密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.ENCRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

    /**
     * Decrypt.
     *
     * @param data the data
     * @param key  the key
     * @return the byte [ ]
     * @throws Exception the exception
     * @author kongqiang
     * @date 2017.03.30
     */
    private static byte[] decrypt(byte[] data, byte[] key) throws Exception {
        // 生成一个可信任的随机数源
        SecureRandom sr = new SecureRandom();
        // 从原始密钥数据创建DESKeySpec对象
        DESKeySpec dks = new DESKeySpec(key);
        // 创建一个密钥工厂，然后用它把DESKeySpec转换成SecretKey对象
        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(DES);
        SecretKey securekey = keyFactory.generateSecret(dks);
        // Cipher对象实际完成解密操作
        Cipher cipher = Cipher.getInstance(DES);
        // 用密钥初始化Cipher对象
        cipher.init(Cipher.DECRYPT_MODE, securekey, sr);
        return cipher.doFinal(data);
    }

}
